package com.cpd.blog.admin.service;

import com.cpd.blog.admin.pojo.Admin;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Component;

import java.util.ArrayList;

/**
 * @ClassName SecurityUserService
 * @date 2021/8/19 20:09
 */
@Component
public class SecurityUserService implements UserDetailsService {

    @Autowired
    private AdminService adminService;

    //登陆时会把 username传到这里
    //通过 username查询 admin表，若 admin存在，则将密码告诉 spring security
    //不存在则返回 null，认证失败
    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {

        Admin admin= this.adminService.finAdminByUserName(username);
        if(admin==null){
            return null;
        }

        UserDetails userDetails= new User(username,admin.getPassword(),new ArrayList<>());
        return userDetails;
    }
}
